import axios from "axios";
import useMemberStore from "@/store/modules/memberStore.ts";
import {message} from 'ant-design-vue';

const memberStore = useMemberStore();

//创建axios实例
let request = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_URL,
    timeout: 10000
})
//请求拦截器
request.interceptors.request.use(config => {
    if (memberStore.getMember().token) {
        config.headers.token = memberStore.getMember().token;
    }
    console.log("请求参数:" + JSON.stringify(config))
    return config;
});
//响应拦截器
request.interceptors.response.use((response) => {
    console.log("响应结果:" + JSON.stringify(useMemberStore))
    return response.data;
}, async (error) => {
    if (error.response.status === 401) {
        memberStore.setMember({})
        await message.success('未登录或登录超时！');
    }
    console.log("响应错误:" + error)
    return Promise.reject(error);
});
export default request;